package com.example.springbootwebsocketmonikehuduan;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.example.springbootwebsocketmonikehuduan.entity.Menjing;
import org.apache.log4j.Logger;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;


/**
 * @program: springboot-websocket-monikehuduan
 * @description: 22
 * @author: XianJin.Lu
 * @create: 2021-09-02 16:47
 **/
public class WebsocketClient333 {

    private String requestBody;

    public WebsocketClient333(String param){
        this.requestBody=param;
    }

    private static Logger logger = Logger.getLogger(WebsocketClient333.class);
    public static WebSocketClient client;
    private static Menjing response00=null;

   int time=10;
    public Menjing find2(){
       find();
  while(time>0){
      if(response00!=null){
          return response00;
      }
      time--;
      try {
          Thread.sleep(1000);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
       return  response00;
 }



    public  void find() {

        String s3= "{\"head\" : { \"cmdType\":1, \"cmdSn\":2222: } ,\"data\": { \"devId\":0 } }";
        String s2= "{head : { \"cmdType\":2, \"cmdSn\":2222: } ,data : { \"devId\":0,\"operType\":0,\"doorIndex\":1 } }";
        String s= "{\"head\" : { \"cmdType\":1, \"cmdSn\":\"13c1d6ba578142b0a18fbab1ac856b50\": } ,\"data\" : { \"devId\":0 } }";


        //测试请求体
        String   s4=   "{'head':{'cmdType':1,'cmdSn':'13c1d6ba578142b0a18fbab1ac856b50'},'data':{'devId':0}}";



        try {

            //测试用  ws://113.116.35.114:55551        现场    ws://10.0.7.21:9008
            //ws://localhost:8081/websocket/10
            client = new WebSocketClient(new URI("ws://10.0.7.21:9008"),new Draft_6455()) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                  logger.info("连接成功");

                  //连接成功发送消息
                    client.send(requestBody);
                    System.out.println(requestBody);
                }


                //这个方法不会阻塞当前线程,当前线程不会等他执行完在执行
                @Override
                public void onMessage(String message) {

               logger.info("收到消息=========="+message);




               // Teacher teacher = JSON.parseObject(message, Teacher.class);
               // System.out.println("转换为teacher实体类==========" + teacher.toString());

                response00 = JSON.parseObject(message, Menjing.class);
            //    System.out.println(response00.toString());

                    if(message.equals("over")){
                        client.close();
                    }

                }

                @Override
                public void onClose(int i, String s, boolean b) {
                   logger.info("链接已关闭");
                }

                @Override
                public void onError(Exception e){
                    e.printStackTrace();
                 logger.info("发生错误已关闭");
                }
            };
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        client.connect();

        logger.info(client.getDraft());
        int i=0;
        while(!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
      //  logger.info("正在连接...");
            i++;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //设置连接超时断开  要不然会一直阻塞在此
            if(i>8){
                System.out.println("连接超时,请联系管理员");
                break;
            }
        }





    }

}

